package com.hsy.sunflowerbao.login.ui

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.flux.action.RxStoreChange
import com.gyf.barlibrary.ImmersionBar
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.ui.activity.BaseActivity
import com.hsy.sunflowerbao.login.action.LoginActionCreator
import com.hsy.sunflowerbao.login.store.LoginStore
import com.hsy.sunflowerbao.common.ui.MainActivity
import com.hsy.sunflowerbao.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_login.*

/**
 * 登录页
 * Created on 2017/12/6.
 *
 * @author Yang
 */
class LoginActivity : BaseActivity<LoginStore>(), View.OnClickListener, TextWatcher {

    /**
     * 事件产生器
     */
    private val creator = LoginActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = LoginStore(dispatcher)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        loginVersion.text = getString(R.string.txt_version, CommonUtils.getVerName(this))

        loginPhone.addTextChangedListener(this)
        loginPassword.addTextChangedListener(this)
        loginCommit.setOnClickListener(this)
    }

    /**
     * 设置状态栏颜色
     */
    override fun setStatusBar() {
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)//设置状态栏颜色
                .statusBarDarkFont(true,0.15f)//设置状态栏文字颜色
                .init()
    }

    /**
     * 点击事件
     */
    override fun onClick(v: View) {
        when (v.id) {
            R.id.loginCommit -> {
                CommonUtils.showProgressDialog(this)
                creator.login(loginPhone.text.toString(), loginPassword.text.toString())
            }
        }
    }

    /**
     * 界面响应统一处理
     * @param change 需要界面响应的变化
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        super.onRxStoreChanged(change)
        if (change.storeId == store.tag) {
            CommonUtils.hideProgressDialog()
//            业务处理
            if (change.success) {
                when (change.type) {
                    ActionType.LOGIN -> {
//                        登录成功
                        CommonUtils.saveToken(store.result.token)
                        CommonUtils.saveUserId(store.result.token)
                        if (store.result.shop_id != "0") {
                            creator.selectShop(store.result.shop_id)
                        } else {
                            startActivity(ShopsActivity::class.java)
                        }
                    }
                    ActionType.SELECT_SHOP -> {
                        CommonUtils.saveSid(store.result.shop_id)
                        CommonUtils.saveShopName(store.result.shop_name)
                        startActivityWithTaskCleared(MainActivity::class.java)
                    }
                }
            }
        }
    }

    /**
     * 输入框文字监听
     * 用于判断注册按钮状态
     * 当手机号格式输入正确且输入密码时，注册按钮处于可用状态。否则注册按钮不可用
     */
    override fun afterTextChanged(s: Editable?) {
//        loginCommit.isEnabled = loginPhone.text.length == 11 && loginPassword.text.isNotEmpty()
        loginCommit.isEnabled = true
    }

    /**
     * 输入框文字监听，无意义
     */
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    /**
     * 输入框文字监听，无意义
     */
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }
}